from manim import *
# 版本manimce v19.0
class EquationSolverSequentialAnimation(Scene):
    def construct(self):
        # 1. 标题引入
        title = Text(r"一元一次方程的求解", font_size=50, color=YELLOW)
        self.play(Write(title))
        self.wait(1)
        title.to_edge(UP)

        # 2. 题目展示
        problem_text = Text(
            "例：若 x 的 5 倍加上 3 等于 x 的 7 倍减去 15，求 x 的值。",
            font_size=30,
            color=WHITE,
        )
        problem_text.next_to(title, DOWN, buff=1.0)
        self.play(Write(problem_text))
        self.wait(2)

        # 3. 列出方程（第一步）
        step1_equation = MathTex("5x + 3 = 7x - 15")
        step1_equation.next_to(problem_text, DOWN, buff=1.0)
        self.play(Write(step1_equation))
        self.wait(2)

       # 4. 移项动画（第二步）
        # 突出需要移动的项,分别选中5x,3,7x和15
        five_x = step1_equation[0][0:2]
        plus_3 = step1_equation[0][3]
        seven_x = step1_equation[0][5:7]
        plus_15 = step1_equation[0][8:10]

        # 第一次突出：常数项
        self.play(Indicate(plus_3, scale_factor=1.5), Indicate(plus_15, scale_factor=1.5))
        self.wait(1)
        # 第二次突出：变量项
        self.play(Indicate(five_x, scale_factor=1.5), Indicate(seven_x, scale_factor=1.5))
        self.wait(1)

        # 明确地写出移项后的新方程
        step2_equation = MathTex("7x - 5x = 15 + 3")
        step2_equation.next_to(step1_equation, DOWN, buff=0.5)
        self.play(Write(step2_equation))
        self.wait(2)

        # 5. 合并同类项（第三步）
        # 突出需要合并的项，并创建两个 VGroup
        terms_to_combine_left = VGroup(step2_equation[0][0:2], step2_equation[0][2], step2_equation[0][3:5])
        terms_to_combine_right = VGroup(step2_equation[0][6:8], step2_equation[0][8], step2_equation[0][9:10])

        rect_left = SurroundingRectangle(terms_to_combine_left, color=BLUE)
        rect_right = SurroundingRectangle(terms_to_combine_right, color=BLUE)
        
        self.play(Create(rect_left), Create(rect_right))
        self.wait(1)

        # 明确地写出合并后的新方程
        step3_equation = MathTex("2x = 18")
        step3_equation.next_to(step2_equation, DOWN, buff=0.5)
        
        # 修正：使用 TransformFromCopy 将左侧合并项复制并转换到新方程的左侧
        self.play(
            TransformFromCopy(terms_to_combine_left, step3_equation[0][0:2]), # 7x - 5x -> 2x
            TransformFromCopy(terms_to_combine_right, step3_equation[0][3:5]), # 15 + 3 -> 18
            # 在转换完成后，再写入等号
            Write(step3_equation[0][2]),
            # 同时将矩形淡出
            FadeOut(rect_left),
            FadeOut(rect_right)
        )
        self.wait(2)
        
        # 6. 求解（第四步）
        # 突出系数2
        coefficient_2 = step3_equation[0][0]
        self.play(Indicate(coefficient_2, scale_factor=1.5))
        self.wait(1)
        
        # 明确地写出最终的解
        final_solution = MathTex("x = 9")
        final_solution.next_to(step3_equation, DOWN, buff=0.5)
        self.play(Write(final_solution))
        self.wait(2)
        
        # 7. 最终结论
        final_text = Text("所以，x 的值为 9", font_size=40, color=GREEN)
        final_text.next_to(final_solution, DOWN, buff=1.0)
        self.play(Write(final_text))
        self.wait(3)